<?php

namespace App\Listener;

use Hyperf\Event\Contract\ListenerInterface;
use Psr\Container\ContainerInterface;
use \App\Service\Admin\ActionLogService;
class ActionLogListener implements ListenerInterface
{
    /**
     * @var ContainerInterface
     */
    private ContainerInterface $container;

    protected ActionLogService $actionLogService;
    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
        $this->actionLogService = $this->container->get(\App\Service\Admin\ActionLogService::class);
    }

    public function listen(): array
    {
        return [
            \App\Event\ActionLog::class
        ];
    }

    /**
     * Notes:处理事件
     * 2025/7/16
     * @param object $event
     * @return void
     */
    public function process(object $event): void
    {
        $services = $event->request->getServerParams();
        $userAgent = $event->request->getHeader('user-agent');
        $log = [
            'user_id' => $event->auth->id,
            'user_name' => $event->auth->name,
            'controller' => $event->controller,
            'action' => $event->action,
            'method' => $event->request->getMethod(),
            'data' => json_encode($event->request->all()),
            'url' => $event->request->getUri(),
            'browser' => getBrowserInfo($userAgent[0]),
            'os' => getOs($userAgent[0]),
            'ip' => $services['remote_addr'],
        ];

        $this->actionLogService->saveLog($log);
    }
}